package com.sailing.xjpb.ipaas.client.core;

import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * ipaas的版本，版本不同，与ipaas交互接口不同
 *
 * @author YaoWei
 * createDate: 2022-2-10
 */
public enum IpaasVersion {

    /**
     * 精简版：部分url需要移除服务名称
     * 支持ipaasV2.0以下（不包含2.0）版本配置
     */
    MINI {
        @Override
        public String getRequestUrI(String requestUri) {
            String uri = requestUri;
            if (!uri.startsWith("/")) {
                uri += "/";
            }
            while (uri.contains("//")) {
                uri = uri.replace("//", "/");
            }
            String[] splitUris = StringUtils.split(uri, "/");
            if (splitUris.length <= 1) {
                return uri;
            }
            String[] subUris = Arrays.copyOfRange(splitUris, 1, splitUris.length);
            String rsUri = Arrays.stream(subUris).skip(0).collect(Collectors.joining("/", "/", ""));
            if (requestUri.endsWith("/")) {
                rsUri = rsUri + "/";
            }
            return rsUri;
        }
    },
    /**
     * 标准版：ipaas2.0版本
     */
    COMMON,
    /**
     * ipaasV2.1以上（含）版本增加了/server/ipaas前缀
     * add by YaoWei since 2023/02/28
     */
    V21 {
        @Override
        public String getRequestUrI(String requestUri) {
            String ipaasContext = "/server/ipaas";
            String newRequestUri = requestUri.contains(ipaasContext) ? requestUri : ipaasContext + "/" + requestUri;
            return newRequestUri.replace("//", "/");
        }
    };

    /**
     * 根据版本不同转换请求地址
     *
     * @param requestUri 请求地址
     * @return 转换后的请求地址
     */
    public String getRequestUrI(String requestUri) {
        return requestUri;
    }


}
